<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('保证金') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('申请标题')" prop="actTitle">
        <el-input v-model="queryParams.actTitle" :placeholder="$t('申请标题')" />
      </el-form-item>
      <el-form-item :label="$t('项目名称')" prop="dealName">
        <el-input v-model="queryParams.dealName" :placeholder="$t('项目名称')" />
      </el-form-item>
      <el-form-item :label="$t('收款方名称')" prop="payeeName">
        <el-input v-model="queryParams.payeeName" :placeholder="$t('收款方名称')" />
      </el-form-item>
      <el-form-item :label="$t('申请人')" prop="nickName">
        <el-input v-model="queryParams.nickName" :placeholder="$t('申请人')" />
      </el-form-item>
      <el-form-item :label="$t('是否归还')" prop="whetherReturn">
        <el-select v-model="queryParams.whetherReturn" clearable value="" :placeholder="$t('是否归还')">
          <el-option
            v-for="dict in sysYesNoOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        <el-button type="warning" icon="el-icon-download" size="mini" @click="exportHandle">{{ $t("导出") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list" border>
      <el-table-column :label="$t('申请标题')" align="center" prop="actTitle" :show-overflow-tooltip="true" min-width="150">
        <template slot-scope="scope">
          <el-link class="link-a" type="primary" @click="openOaPaymentForm(scope.row)">{{ scope.row.actTitle }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('项目名称')" align="center" show-overflow-tooltip min-width="150">
        <template slot-scope="scope">
          <router-link
            target="_blank"
            :to="{path:'/deal/home/dealBaseHome',query:{id:scope.row.dealId,companyId:scope.row.companyId, gpId: scope.row.gpId}}"
            class="link_a"
          >{{ scope.row.dealName }}</router-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('收款方名称')" align="center" prop="payeeName" show-overflow-tooltip min-width="150" />
      <el-table-column :label="$t('申请人')" align="center" prop="nickName" show-overflow-tooltip min-width="120" />
      <el-table-column :label="$t('申请时间')" align="center" prop="applyTime" show-overflow-tooltip min-width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('付款金额(元)')" align="center" prop="amount" min-width="140">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.amount) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('归还金额')" align="center" prop="returnAmount" min-width="140">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.returnAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('是否归还')" align="center" prop="whetherReturn" show-overflow-tooltip min-width="110" :formatter="whetherReturnFormatter" />
      <el-table-column :label="$t('归还时间')" align="center" prop="returnDate" show-overflow-tooltip min-width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" fixed="right" width="100px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="'oa:earnestMoney:edit'"
            size="small"
            type="text"
            icon="el-icon-money"
            @click="returnAmountBtn(scope.row)"
          >{{ $t('归还') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog v-el-drag-dialog :title="$t('字典类型')" :close-on-click-modal="false" :visible.sync="formOpen" width="400px">
      <el-form ref="form" :model="form" :rules="rules">
        <el-col :span="24">
          <el-form-item :label="$t('归还金额(元)')" prop="returnAmount">
            <Money v-model="form.returnAmount" :placeholder="$t('归还金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('归还时间')" prop="returnDate">
            <el-date-picker
              v-model="form.returnDate"
              style="width: 100%"
              type="date"
              :placeholder="$t('归还时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('是否归还')" prop="whetherReturn">
            <el-select v-model="form.whetherReturn" style="width: 100%" disabled :placeholder="$t('是否归还')">
              <el-option
                v-for="dict in sysYesNoOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">{{ $t("取消") }}</el-button>
        <el-button v-hasPermi="'oa:earnestMoney:edit'" type="primary" @click="submitForm">{{ $t("确定") }}</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="$t('对外付款')" :visible.sync="paymentOpen" width="70%" :close-on-click-modal="false" custom-class="dialogForm" :lock-scroll="false">
      <oaPaymentForm ref="oaPaymentForm" />
    </el-dialog>
  </div>
</template>

<script>

import {
  getEarnestMoneyByPaymentTypeId,
  getEarnestMoneyList,
  saveAnxinEarnestMoney
} from '@/api/oa/payment/anxinEarnestMoney'
import oaPaymentForm from '@/views/oa/payment/oaPaymentForm'

export default {
  name: 'EquityBriefTableReport',
  components: {
    oaPaymentForm
  },
  data() {
    return {
      list: [],
      total: 0,
      formOpen: false,
      paymentOpen: false,
      loading: false,
      // 弹出层标题
      title: '归还保证金',
      sysYesNoOptions: [], // 系统是否
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        returnAmount: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        returnDate: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        whetherReturn: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dealName: undefined,
        nickName: undefined,
        whetherReturn: undefined,
        payeeName: undefined,
        actTitle: undefined,
        subjectCode: 'earnest_money'
      }
    }
  },
  mounted() {
    this.getDicts('sys_yes_no').then(response => {
      this.sysYesNoOptions = response.data
    })
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      getEarnestMoneyList(this.queryParams).then(res => {
        this.list = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    // 系统是否字典翻译
    whetherReturnFormatter(row) {
      return this.selectDictLabel(this.sysYesNoOptions, row.whetherReturn)
    },
    /** 修改按钮操作 */
    openOaPaymentForm(row) {
      this.paymentOpen = true
      this.$nextTick(() => {
        const data = {
          id: row.paymentId
        }
        this.$refs.oaPaymentForm.init(data)
        this.$refs.oaPaymentForm.$refs.form.clearValidate()
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/earnestMoney/anxinEarnestMoney/reportExport', this.queryParams)
    },
    /** 归还 */
    returnAmountBtn(row) {
      console.log('归还保证金', row)
      if (row.whetherReturn === 'Y') {
        // 根据对外付款ID获取保证金记录
        getEarnestMoneyByPaymentTypeId(row.id).then(response => {
          this.form = response.data
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.form.paymentTypeId = row.id
        this.form.whetherReturn = 'Y'
      }
      this.formOpen = true
    },
    // 取消按钮
    cancel() {
      this.formOpen = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        paymentTypeId: undefined,
        returnAmount: undefined,
        returnDate: undefined,
        whetherReturn: 'Y',
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveAnxinEarnestMoney(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.formOpen = false
              this.getList()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      }).catch(() => {
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    }
  }
}
</script>

<style scoped>

</style>
